package com.dwd.pet.service.impl;

import com.dwd.basic.constant.PetLoginConstant;
import com.dwd.basic.service.impl.BaseServiceImpl;
import com.dwd.basic.utils.CodeGenerateUtils;
import com.dwd.basic.utils.SystemContext;
import com.dwd.order.mapper.PetAcquisitionOrderMapper;
import com.dwd.order.pojo.PetAcquisitionOrder;
import com.dwd.org.mapper.EmployeeMapper;
import com.dwd.org.mapper.ShopMapper;
import com.dwd.org.pojo.Employee;
import com.dwd.org.pojo.Shop;
import com.dwd.pet.mapper.PetDetailMapper;
import com.dwd.pet.mapper.PetMapper;
import com.dwd.pet.mapper.SearchMasterMsgMapper;
import com.dwd.pet.pojo.Pet;
import com.dwd.pet.pojo.PetDetail;
import com.dwd.pet.pojo.SearchMasterMsg;
import com.dwd.pet.service.IPetService;
import com.dwd.user.pojo.LoginInfo;
import com.dwd.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/24 19:55
 * @Version 1.0
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 处理寻主信息,存储到pet和petdetail
     *
     * @param pet
     * @param httpRequest
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePet(Pet pet, HttpServletRequest httpRequest) {
        //得到后台登陆的用户
        LoginInfo loginInfo = SystemContext.getLoginInfo(httpRequest);
        //根据logininfo查询当前用户是否有shopId，如果有就是店铺管理员或员工
        Long shopId = shopMapper.queryShopIdByLoginInfo(loginInfo);
        if (shopId != null) {
            pet.setShop_id(shopId);
        }
        //得到发布信息的门户用户
        User user = pet.getUser();
        pet.setUser_id(user.getId());
        //查询对应店铺
        Shop shop = shopMapper.queryById(pet.getShop_id());
        pet.setShop(shop);
        //设置来源 1 为寻主
        pet.setSourceType(PetLoginConstant.SOURCE);
        //先存pet，返回自增id
        petMapper.save(pet);
        PetDetail detail = pet.getDetail();

        Long petId = pet.getId();
        detail.setPet_id(petId);
        petDetailMapper.save(detail);
        pet.setDetail(detail);
        //还需要将寻主信息的表状态改为1,已经处理
        searchMasterMsgMapper.updateState(pet.getAdopt_id());
        //查询寻主信息
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.queryById(pet.getAdopt_id());
        //创建订单
        PetAcquisitionOrder petAcquisitionOrder = createPetAcquisitionOrder(pet, searchMasterMsg, loginInfo);
        petAcquisitionOrderMapper.save(petAcquisitionOrder);


    }

    /**
     * 创建收购订单
     *
     * @param pet
     * @param searchMasterMsg
     * @param loginInfo
     * @return
     */
    private PetAcquisitionOrder createPetAcquisitionOrder(Pet pet, SearchMasterMsg searchMasterMsg, LoginInfo loginInfo) {
        PetAcquisitionOrder petAcquisitionOrder = new PetAcquisitionOrder();
        Employee employee = employeeMapper.queryEmpByLoginInfoId(loginInfo.getId());
        petAcquisitionOrder.setDigest("[摘要]对" + pet.getName() + "收购订单！")
                //如果价格为0 订单结束3
                .setState(pet.getCostprice().equals(BigDecimal.ZERO) ? PetLoginConstant.PETACQUISITION_ORDER_COMPLETE : pet.getPaytype())
                .setPrice(pet.getCostprice())
                .setAddress(searchMasterMsg.getAddress())
                .setOrderSn(CodeGenerateUtils.generateOrderSn(pet.getUser().getId()))
                .setPet(pet)
                .setUser(pet.getUser())
                .setPaytype(pet.getPaytype())
                .setShop(pet.getShop())
                .setEmployee(employee)
                //状态为3 订单时间结束
                .setLastcomfirmtime(petAcquisitionOrder.getState().equals(PetLoginConstant.PETACQUISITION_ORDER_COMPLETE) ? new Date() : null);
        return petAcquisitionOrder;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        //查询当条数据
        Pet pet = petMapper.queryById(id);
        //删除pet
        super.delete(id);
        //删除详情
        petDetailMapper.delete(pet.getDetail().getId());
        //如果是寻主就把寻主信息删掉
        if (pet.getAdopt_id() != null) {
            searchMasterMsgMapper.delete(pet.getAdopt_id());
        }

    }

    /**
     * 批量上架
     *
     * @param pets
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUp(List<Pet> pets) {
        petMapper.batchUp(pets);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDown(List<Pet> pets) {
        petMapper.batchDown(pets);
    }

    /**
     * 增加
     *
     * @param pet
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdatePet(Pet pet) {
        //修改
        if (pet.getId() != null) {
            Long typyId = pet.getType().getId();
            pet.setType_id(typyId);
            petMapper.update(pet);

            PetDetail detail = pet.getDetail();
            detail.setPet_id(pet.getId());
            //存储详情
            petDetailMapper.update(detail);
        } else {
            //因为sql写的是type_id
            Long typyId = pet.getType().getId();
            pet.setType_id(typyId);
            //存储pet
            petMapper.save(pet);
            PetDetail detail = pet.getDetail();
            detail.setPet_id(pet.getId());
            //存储详情
            petDetailMapper.save(detail);
        }
    }
}
